<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Structured forest training</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d3/d81/tutorial_contrib_root.html">Tutorials for contrib modules</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Structured forest training </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h2>Introduction </h2>
<p>In this tutorial we show how to train your own structured forest using author's initial Matlab implementation.</p>
<h2>Training pipeline </h2>
<ol type="1">
<li>Download "Piotr's Toolbox" from <a href="http://vision.ucsd.edu/~pdollar/toolbox/doc/index.html">link</a> and put it into separate directory, e.g. PToolbox</li>
<li>Download BSDS500 dataset from link &lt;<a href="http://www.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/BSR/">http://www.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/BSR/</a>&gt; and put it into separate directory named exactly BSR</li>
<li>Add both directory and their subdirectories to Matlab path.</li>
<li>Download detector code from link &lt;<a href="http://research.microsoft.com/en-us/downloads/389109f6-b4e8-404c-84bf-239f7cbf4e3d/">http://research.microsoft.com/en-us/downloads/389109f6-b4e8-404c-84bf-239f7cbf4e3d/</a>&gt; and put it into root directory. Now you should have : <div class="fragment"><div class="line">.</div><div class="line">    BSR</div><div class="line">    PToolbox</div><div class="line">    models</div><div class="line">    private</div><div class="line">    Contents.m</div><div class="line">    edgesChns.m</div><div class="line">    edgesDemo.m</div><div class="line">    edgesDemoRgbd.m</div><div class="line">    edgesDetect.m</div><div class="line">    edgesEval.m</div><div class="line">    edgesEvalDir.m</div><div class="line">    edgesEvalImg.m</div><div class="line">    edgesEvalPlot.m</div><div class="line">    edgesSweeps.m</div><div class="line">    edgesTrain.m</div><div class="line">    license.txt</div><div class="line">    readme.txt</div></div><!-- fragment --></li>
<li>Rename models/forest/modelFinal.mat to models/forest/modelFinal.mat.backup</li>
<li>Open edgesChns.m and comment lines 26&ndash;41. Add after commented lines the following: <div class="fragment"><div class="line">shrink=opts.shrink;</div><div class="line">chns = single(getFeatures( im2double(I) ));</div></div><!-- fragment --></li>
<li>Now it is time to compile promised getFeatures. I do with the following code: <div class="fragment"><div class="line"><span class="preprocessor">#include &lt;cv.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;highgui.h&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;mat.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;mex.h&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;MxArray.hpp&quot;</span> <span class="comment">// https://github.com/kyamagu/mexopencv</span></div><div class="line"></div><div class="line"><span class="keyword">class </span>NewRFFeatureGetter : <span class="keyword">public</span> cv::RFFeatureGetter</div><div class="line">{</div><div class="line"><span class="keyword">public</span>:</div><div class="line">    NewRFFeatureGetter() : name(<span class="stringliteral">&quot;NewRFFeatureGetter&quot;</span>){}</div><div class="line"></div><div class="line">    <span class="keyword">virtual</span> <span class="keywordtype">void</span> getFeatures(<span class="keyword">const</span> <a class="code" href="../../d3/d63/classcv_1_1Mat.html">cv::Mat</a> &amp;src, NChannelsMat &amp;features,</div><div class="line">                             <span class="keyword">const</span> <span class="keywordtype">int</span> gnrmRad, <span class="keyword">const</span> <span class="keywordtype">int</span> gsmthRad,</div><div class="line">                             <span class="keyword">const</span> <span class="keywordtype">int</span> shrink, <span class="keyword">const</span> <span class="keywordtype">int</span> outNum, <span class="keyword">const</span> <span class="keywordtype">int</span> gradNum)<span class="keyword"> const</span></div><div class="line"><span class="keyword">    </span>{</div><div class="line">        <span class="comment">// here your feature extraction code, the default one is:</span></div><div class="line">        <span class="comment">// resulting features Mat should be n-channels, floating point matrix</span></div><div class="line">    }</div><div class="line"></div><div class="line"><span class="keyword">protected</span>:</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">cv::String</a> name;</div><div class="line">};</div><div class="line"></div><div class="line">MEXFUNCTION_LINKAGE <span class="keywordtype">void</span> mexFunction(<span class="keywordtype">int</span> nlhs, mxArray *plhs[], <span class="keywordtype">int</span> nrhs, <span class="keyword">const</span> mxArray *prhs[])</div><div class="line">{</div><div class="line">    <span class="keywordflow">if</span> (nlhs != 1) mexErrMsgTxt(<span class="stringliteral">&quot;nlhs != 1&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (nrhs != 1) mexErrMsgTxt(<span class="stringliteral">&quot;nrhs != 1&quot;</span>);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">cv::Mat</a> src = MxArray(prhs[0]).toMat();</div><div class="line">    src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#adf88c60c5b4980e05bb556080916978b">convertTo</a>(src, <a class="code" href="../../d0/d3a/classcv_1_1DataType.html">cv::DataType&lt;float&gt;::type</a>);</div><div class="line"></div><div class="line">    std::string modelFile = MxArray(prhs[1]).toString();</div><div class="line">    NewRFFeatureGetter *pDollar = createNewRFFeatureGetter();</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">cv::Mat</a> edges;</div><div class="line">    pDollar-&gt;getFeatures(src, edges, 4, 0, 2, 13, 4);</div><div class="line">    <span class="comment">// you can use other numbers here</span></div><div class="line"></div><div class="line">    edges.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#adf88c60c5b4980e05bb556080916978b">convertTo</a>(edges, <a class="code" href="../../d0/d3a/classcv_1_1DataType.html">cv::DataType&lt;double&gt;::type</a>);</div><div class="line"></div><div class="line">    plhs[0] = MxArray(edges);</div><div class="line">}</div></div><!-- fragment --></li>
<li>Place compiled mex file into root dir and run edgesDemo. You will need to wait a couple of hours after that the new model will appear inside models/forest/.</li>
<li>The final step is converting trained model from Matlab binary format to YAML which you can use with our ocv::StructuredEdgeDetection. For this purpose run opencv_contrib/ximgproc/tutorials/scripts/modelConvert(model, "model.yml")</li>
</ol>
<h2>How to use your model </h2>
<p>Just use expanded constructor with above defined class NewRFFeatureGetter </p><div class="fragment"><div class="line">cv::StructuredEdgeDetection pDollar</div><div class="line">    = <a class="code" href="../../de/d51/group__ximgproc__edge.html#ga39029b3c6691b0314b53c68c79d5b788">cv::createStructuredEdgeDetection</a>( modelName, makePtr&lt;NewRFFeatureGetter&gt;() );</div></div><!-- fragment --> </div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:40 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
